import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:m2_flutter/data/config.dart';
import 'package:m2_flutter/service/SqlManager.dart';
import 'package:m2_flutter/util/util.dart';
import 'package:m2_flutter/widget/TitleBar.dart';

import '../data/method_bean.dart';

class InputPage extends StatefulWidget {
  final MethodBean bean;
  const InputPage({Key? key, required this.bean}) : super(key: key);
  @override
  _InputPageState createState() => _InputPageState();
}

class _InputPageState extends State<InputPage> {
  final _money_controller = TextEditingController();
  final _reason_controller = TextEditingController();
  var date = DateTime.now();
  @override
  void dispose() {
    super.dispose();
    _money_controller.dispose();
    _reason_controller.dispose();
  }

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () {
      init();
    });
  }

  Future<void> init() async {
    if (widget.bean.type == "update") {
      var map = await SqlManager.getById(int.parse(widget.bean.id.toString()));
      _money_controller.text = map["money"];
      _reason_controller.text = map["reason"];
      date = DateTime(int.parse(map["year"]), int.parse(map["month"]),
          int.parse(map["day"]));
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        statusBarIconBrightness: Brightness.dark));
    return Scaffold(
        body: SafeArea(
      child: SingleChildScrollView(
        child: Column(children: [
          TitleBar(context, "记一笔", false),
          Image.asset(
            'assets/logo.png',
            scale: 3,
          ),
          Padding(
            padding: const EdgeInsets.only(top: 50),
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
              child: TextField(
                controller: _money_controller,
                keyboardType: TextInputType.number,
                inputFormatters: [
                  FilteringTextInputFormatter.deny(RegExp("[\u4e00-\u9fa5]")),
                ],
                cursorColor: Config.color_main,
                decoration: InputDecoration(
                    enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Config.color_main),
                      borderRadius: const BorderRadius.all(Radius.circular(5)),
                    ),
                    focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Config.color_main),
                      borderRadius: const BorderRadius.all(Radius.circular(5)),
                    ),
                    labelText: "金额",
                    labelStyle: TextStyle(color: Config.color_main),
                    hintText: "请输入金额",
                    prefixIcon: Icon(
                      Icons.money,
                      color: Config.color_main,
                    )),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
            child: TextField(
              maxLines: 5,
              minLines: 1,
              controller: _reason_controller,
              cursorColor: Config.color_main,
              decoration: InputDecoration(
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Config.color_main),
                    borderRadius: const BorderRadius.all(Radius.circular(5)),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: Config.color_main),
                    borderRadius: const BorderRadius.all(Radius.circular(5)),
                  ),
                  labelText: "事由(可选)",
                  labelStyle: TextStyle(color: Config.color_main),
                  hintText: "请输入原因(可选)",
                  prefixIcon: Icon(
                    Icons.question_mark_rounded,
                    color: Config.color_main,
                  )),
            ),
          ),
          InkWell(
            onTap: () async {
              DateTime? result = await showDatePicker(
                context: context,
                firstDate: DateTime(2000),
                initialDate: DateTime(2022),
                lastDate: DateTime.now(),
                initialEntryMode: DatePickerEntryMode.calendarOnly,
              );
              setState(() {
                date = result!;
              });
            },
            child: Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
                child: Row(children: [
                  const Text("日期:"),
                  Text("${date.year}年${date.month}月${date.day}日"),
                ])),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 50),
            child: Container(
              decoration: BoxDecoration(
                  color: Config.color_orange,
                  borderRadius: const BorderRadius.all(Radius.circular(5))),
              child: InkWell(
                onTap: () async {
                  if (_money_controller.text.isEmpty) {
                    Util.toast("请输入金额");
                  } else {
                    if (widget.bean.type == "update") {
                      await SqlManager.update(
                          date: date,
                          money: _money_controller.text,
                          reason: _reason_controller.text,
                          id: widget.bean.id.toString());
                    } else {
                      await SqlManager.add(
                          date: date,
                          money: _money_controller.text,
                          reason: _reason_controller.text);
                    }
                    Util.toast("已保存");
                    Navigator.pop(context);
                  }
                },
                child: const Padding(
                  padding: EdgeInsets.symmetric(horizontal: 100, vertical: 15),
                  child: Text("完成", style: TextStyle(color: Colors.white)),
                ),
              ),
            ),
          )
        ]),
      ),
    ));
  }
}
